
Partial Class TableD
    Inherits System.Web.UI.Page

    Private Const Q As String = Chr(34)

    Private moServer As SQLAutoDocLib.Server = Nothing
    Private moDatabase As SQLAutoDocLib.Database = Nothing
    Private moOwner As SQLAutoDocLib.Login = Nothing
    Private moTable As SQLAutoDocLib.Table = Nothing

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim sSID As String
        Dim sDID As String
        Dim sOID As String
        Dim sREV As String = ""
        Dim sIsFramed As String = ""

        If IsPostBack = False Then
            sSID = Request.QueryString("S")
            sDID = Request.QueryString("D")
            sOID = Request.QueryString("O")
            sREV = Request.QueryString("R")
            sIsFramed = Request.QueryString("F")

            hidSID.Value = sSID
            hidDID.Value = sDID
            hidOID.Value = sOID
            hidRID.Value = sREV
            hidF.value = sIsFramed
        Else
            hidRID.Value = Request.Form("cmbRev")
            sREV = Request.Form("cmbRev")

            sSID = hidSID.Value
            sDID = hidDID.Value
            sOID = hidOID.Value
            sIsFramed = hidF.Value
        End If

        moServer = New SQLAutoDocLib.Server(sSID)
        moServer.Load()

        moDatabase = New SQLAutoDocLib.Database(sDID)
        moDatabase.Load()

        If Not IsNumeric(sREV) Then
            moTable = New SQLAutoDocLib.Table(sOID)
        Else
            moTable = New SQLAutoDocLib.Table(sOID, sREV)
        End If
        moTable.Load()

        moOwner = New SQLAutoDocLib.Login(moTable.OwnerID)
        moOwner.Load()

        If IsPostBack = False Then
            If Not IsNumeric(sIsFramed) Then
                Dim oRevFactory As New SQLAutoDocLib.Table_Factory
                Dim oList As ArrayList = oRevFactory.GetAllTableRevs(moTable.ID)
                For Each oRev As SQLAutoDocLib.Table In oList
                    cmbRev.Items.Add(New ListItem(oRev.RevDate, oRev.RevID))
                Next

                hidRID.Value = oList.Item(0).RevID
                txtDesc.Value = moTable.Desc

            Else
                cmbRev.Visible = False
                lblRev.Visible = True

                lblRev.InnerText = moTable.CreateDate
                txtDesc.Visible = False

                If moTable.Desc > "" Then
                    lblDesc.Style.Add("border", "solid 1px grey")

                    lblDesc.InnerHtml = "<pre>" & moTable.Desc & "</pre>"
                Else
                    lblDesc.Style.Add("height", "1px")
                End If
            End If

        ElseIf hidToolbarAction.Value = "refresh" Then
            txtDesc.Value = moTable.Desc

        ElseIf hidToolbarAction.Value = "save" Then
            moTable.Desc = txtDesc.Value
            moTable.Save()

            Dim oExtendedProperties As New SQLAutoDocLib.ExtendedProperties
            oExtendedProperties.UpdateTableDescription(moTable.DatabaseID, moTable.ID, txtDesc.Value)

            Dim sFieldIDs() As String = Request.Form("FID").Split(",")
            For Each sID As String In sFieldIDs
                If IsNumeric(sID) Then
                    Dim sFDesc As String = Request.Form("desc:" & sID)
                    If sFDesc > "" Then
                        Dim oField As New SQLAutoDocLib.Field(sID)
                        oField.Load()
                        oField.Desc = sFDesc
                        oField.Save()

                        oExtendedProperties.UpdateFieldDescription(moTable.DatabaseID, oField.ID, sFDesc)
                    End If
                End If
            Next

        ElseIf hidToolbarAction.Value = "back" Then
            Server.Transfer("SQTableE.aspx?S=" + hidSID.Value & "&D=" & hidDID.Value)
        End If
    End Sub

    Protected Sub ShowTableDetail()
        Dim lTableID As Long = hidOID.Value
        Dim lRevID As Long = hidRID.Value

        Dim oTable As New SQLAutoDocLib.Table(lTableID, lRevID)
        oTable.Load()

        Dim bAllowEdit As Boolean = False
        If Not IsNumeric(hidF.Value) Then
            bAllowEdit = True
        End If

        Dim oFieldReporter As New SQLAutoDocLib.Rpt_Fields
        oFieldReporter.ShowFieldDetail(lTableID, lRevID, bAllowEdit)

        Dim oIndexReporter As New SQLAutoDocLib.Rpt_Indexes
        oIndexReporter.ShowIndexDetail(lTableID, lRevID)
    End Sub

    Protected Function FormLabel() As String
        Return "Table: " & moOwner.Login & "." & moTable.Name
    End Function

    Protected Function ScanMode() As String
        Return "Tables"
    End Function

    Protected Function EditMode() As String
        Return "Table"
    End Function

    Protected Function ServerID() As String
        Return moServer.ID
    End Function

    Protected Function DatabaseID() As String
        Return moDatabase.ID
    End Function

    Protected Function ObjectID() As String
        Return moTable.ID
    End Function

    Protected Function ObjectRev() As String
        Return moTable.RevID
    End Function


End Class
